<?php
/**
 * 百度搜索自动补全
 * Created by PhpStorm.
 * User: g
 * Date: 2018/1/9
 * Time: 10:23
 */

namespace app\index\controller;


class Bs extends Common
{
    //分区
    public function zoning(){
        $data = db('cg_market_zoning')->field('id,title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //省份
    public function provice(){
        $data = db('cg_market_provice')->field('id,title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //城市
    public function city(){
        $data = db('cg_market_city')->field('id,title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //经销商
    public function jxs(){
        $data = db('cg_jxs')->field('id,title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //品牌
    public function brand(){
        $data = db('pro_brands')->field('id,brand_name title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //品类
    public function category(){
        $brand = $this->request->param('brand');
        $where = [];
        if($brand){
            $where['brand_id'] = $brand;
        }
        $data = db('pro_category')->field('id,category_name title')->where($where)->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //系列
    public function series(){
        $where = [];
        $data = db('pro_series')->field('id,series_name title')->where($where)->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //sku
    public function sku(){
        $where = [];
        $data = db('pro_sku')->field('id,sku_name title')->where($where)->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //单品
    public function products(){
        $where = [];
        $data = db('pro_products')->field('id,pro_name title,unit,pro_no')->where($where)->select();
        foreach($data as $k=>$v){
            $v['lingshou_price'] = db('pro_price')->where('pid',$v['id'])->value('factory');
            $data[$k] = $v;
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //出库物料或成品
    public function kc(){
        $wllx = $this->request->param('wllx');
        $ckdxlx = $this->request->param('ckdxlx');
        $ckdx = $this->request->param('ckdx');
        if($wllx==3){ //成品
            $data = db('pro_products')->field('id,pro_name wlmc,box_volume tj,box_weight zl,pro_no wlbh')->select();
            foreach($data as $k=>$v){
                $v['kcdw'] = '箱';
                if($ckdxlx==1){
                    $gids = db('price_group_jxs')->where('jid',$ckdx)->column('gid');
                    $fayun_price = db('price_group_product')->where(['gid'=>['in',$gids],'pid'=>0,'start_time'=>['<=',date('Y-m-d')],'end_time'=>['>=',date('Y-m-d')]])->value('fayun_price');
                    if($fayun_price){
                        $v['dj'] = $fayun_price;
                        $data[$k] = $v;
                        continue;
                    }
                }
                $v['dj'] = db('pro_price')->where('id',$v['id'])->value('box_factory');
                $data[$k] = $v;
            }

        }elseif($wllx==2){ //生产物料
            $data = db('pro_materials')->where(['m_type'=>1])->field('id,material_name wlmc,unit kcdw,factory dj,volume tj,weight zl,material_no wlbh')->select();
        }else{ //销售物料
            $data = db('pro_materials')->where(['m_type'=>2])->field('id,material_name wlmc,unit kcdw,factory dj,volume tj,weight zl,material_no wlbh')->select();
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //查询所有成品--销售订单专用
    public function product(){
        $jxs_id = $this->request->param('jxs_id');

        if(!$jxs_id){ //没选择经销商返回false
            return false;
        }

        //查询产品订购设置
        $dgsz = db('system_param')->where(['t'=>0,'k'=>'dingDanListType'])->value('v');

        //根据经销商查询分组
        $group_id = db('price_group_jxs')->where('jid',$jxs_id)->column('gid');

        //查询经销商对应的渠道类型
        $qdlx = db('cg_jxs')->where('id',$jxs_id)->value('channeltype');

        //构建查询条件
        $where = [];
        if($dgsz==2){ //经销商对应的产品价格分组中的产品
            $pids = db('price_group_product')
                ->where([
                    'gid'=>['in',$group_id],
                    'start_time'=>['<=',date('Y-m-d')],
                    'end_time'=>['>=',date('Y-m-d')]
                ])
                ->column('pid');
            $where['id'] = ['in',$pids];
        }elseif($dgsz==1){ //经销商对应渠道类型的产品

            $where['channel_type'] = $qdlx;
        }
        $data = db('pro_products')->field('id,pro_name,pro_no,base_unit_id unit,unit u')->where($where)->select();
        foreach($data as $k=>$v){
            if($dgsz==2){ //下单的时候只能选择该经销商所对应的产品价格分组内的产品。
                $price = db('price_group_product')
                    ->where([
                        'gid'=>['in',$group_id],
                        'pid'=>$v['id'],
                        'start_time'=>['<=',date('Y-m-d')],
                        'end_time'=>['>=',date('Y-m-d')]
                    ])
                    ->field('lingshou_price,fayun_price')->find();
                if($v["unit"]==1)
                    $v['lingshou_price'] = $price['fayun_price'];
                else
                    $v['lingshou_price'] = $price['lingshou_price'];

            }else{ //如果下单的产品在该经销商对应的价格分组中有价格，就遵循价格分组的价格。如果没有，则按照产品的标准出厂价
                $price = db('price_group_product')
                    ->where([
                        'gid'=>['in',$group_id],
                        'pid'=>$v['id'],
                        'start_time'=>['<=',date('Y-m-d')],
                        'end_time'=>['>=',date('Y-m-d')]
                    ])
                    ->field('lingshou_price,fayun_price')->find();
                if($price){
                    if($v["unit"]==1)
                        $v['lingshou_price'] = $price['fayun_price'];
                    else
                        $v['lingshou_price'] = $price['lingshou_price'];
                }else{
                    //查询产品标准价格
                    if($v["unit"]==1)
                        $bzj = db('pro_price')->where('pid',$v['id'])->value('box_factory');
                    else
                        $bzj = db('pro_price')->where('pid',$v['id'])->value('factory');
                    $v['lingshou_price'] = $bzj;
                }
            }
            if($v['unit']==1){
                $v['unit'] = '箱';
            }else{
                $v['unit'] = $v["u"];
            }
            $data[$k] = $v;
//            $v['ygj'] = db('')
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //销售人员
    public function xsry(){
        $data = db('employee')->field('id,em_name')->select();
        foreach($data as $k=>$v){
            $v['ssbm'] = db('department t')->join('department_employee t2','t.id=t2.department_id')->where(['t2.index'=>1,'employee_id'=>$v['id']])->value('t.de_name');
            $v['ssbm_id'] = db('department_employee')->where(['index'=>1,'employee_id'=>$v['id']])->value('department_id');
            $data[$k]=$v;
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //销售人员
    public function employee(){
        $data = db('employee')->field('id,em_name title')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //核销sn--销售订单申请--补货
    public function hexiaosn(){
        $jxs_id = $this->request->param('jxs_id');
        if(!$jxs_id){
            return false;
        }
        $cs = db('system_param')->where(['t'=>0,'k'=>'buHuoHeXiaoDanFanWei'])->value('v');
        $where ="h.jxs_id={$jxs_id} and h.status=7 and h.tax>h.yzfje";
        if($cs===0){
            $where.="and hd.zhifufangshi=13";
        }
        
        $datas = db('cg_ti_hexiao h')->join("cg_ti_hexiao_list hd","hd.hx_id=h.id")->where($where)->group("h.id")->field('h.id,h.sn')->select();
        $data=[];
        foreach($datas as $k=>$v){
            if($cs==0){
                $ybh=db("cg_ti_hexiao_zhifu")->where("zftype=2 and hx_id={$v["id"]}")->sum("bczfje");
                if(!$ybh)
                    $ybh=0;
                $tmp=db("cg_ti_hexiao")->where(["id"=>$v["id"]])->find();
                $yue=$tmp["tax"]-$tmp["yzfje"];
                $hb=db("cg_ti_hexiao_list")->where(["hx_id"=>$v["id"],"zhifufangshi"=>13])->sum("shijitourujine");
                $buhuoyue=$hb-$ybh;
                if($buhuoyue>0)
                    $data[]=$v;
            }else{
                $data[]=$v;
            }
           
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //查询所有销售物料--销售订单-货补明细
    public function xswl(){

        $where = [];
        $where['m_type'] = 2;

        $data = db('pro_materials')->field('id,material_name pro_name,material_no pro_no,unit,factory lingshou_price')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //查询所有销售物料--销售订单-订单明细
    public function dd_xswl(){
        $jxs_id = $this->request->param('jxs_id');

        if(!$jxs_id){ //没选择经销商返回false
            return false;
        }

        $where = [];
        $where['m_type'] = 2;

        $data = db('pro_materials')->field('id,material_name pro_name,material_no pro_no,unit,factory lingshou_price')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }


    //核销sn--销售订单申请--补费
    public function hexiaosn_bufei(){
        $jxs_id = $this->request->param('jxs_id');
        if(!$jxs_id){
            return false;
        }

        $where['jxs_id'] = $jxs_id;
        $data = db('cg_ti_hexiao')
            ->field('id,sn')
            ->where($where)
            ->select();
        foreach($data as $k=>$v){
            $zhxje = db('cg_ti_hexiao_list')->where(['hx_id'=>$v['id']])->sum('shijitourujine');
            $yzf = db('cg_ti_hexiao_zhifu')->where(['hx_id'=>$v['id']])->sum('bczfje');
            $ybf = db('cg_order_list')->where(['hxdmxbh'=>$v['id']])->sum('bfje');
            $v['hxje'] = round($zhxje-$yzf-$ybf,2);//需补费的核销金额
            $data[$k]=$v;
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }


    public function users(){
        $data = db('users t')->field('id,login_name')->select();
        foreach($data as $k=>$v){
            $data[$k]=$v;
        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //suggest查找网点
    public function net(){
        $where = [];
        $jxs_id = $this->request->param('jxs_id');
        if($jxs_id){
            $sys_ids = db('cg_system')->where('jxs',$jxs_id)->column('id');
            $where['system_id'] = ['in',$sys_ids];
        }
        $data = db('cg_net')->field('title,id')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //查询所有生产物料
    public function scwl(){

        $where = [];
        $where['m_type'] = 1;

        $data = db('pro_materials')->field('id,material_name title,material_no pro_no,unit,factory lingshou_price')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //查询所有销售物料
    public function xswl2(){

        $where = [];
        $where['m_type'] = 2;

        $data = db('pro_materials')->field('id,material_name title,material_no pro_no,unit,factory lingshou_price')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //查询所有供应商
    public function gys(){
        $type_id = $this->request->param('type_id');
        $where = [];
        $where['type_ids'] = ['like','%"'.$type_id.'"%'];
        $data = db('gys')->field('id,gys_name title')->where($where)->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //特价补差类型
    public function tejia(){
        $where=["freekmlx"=>4];
        $arr=db("cg_feiyongkemu3")->where($where)->field("id,title,tejiabuchatype")->select();
        echo json_encode($arr);
    }
}